<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="specialFormRules" :model="specialForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="specialForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="specialForm.title" />
      </el-form-item>
      <el-form-item prop="cover" label="封面" style="margin-bottom: 30px;">
        <Upload v-model="specialForm.cover" />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-tag
          :key="index"
          v-for="(keyword,index) in specialForm.keyword"
          closable
          :disable-transitions="false"
          style="margin-right:10px; margin-bottom:10px;"
          @close="handleKeywordRemove(index)">
          {{keyword}}
        </el-tag>
        <el-input
          v-if="keywordFormHidden == false"
          v-model="keywordForm.title"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleKeywordConfirm"
          @blur="handleKeywordConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="handleKeywordHidden">+新建</el-button>
      </el-form-item>

      
      <el-form-item label="责任编辑" prop="employeeID">
          <el-select v-model="specialForm.employeeID" placeholder="请选择">
            <el-option
              v-for="item in employeeOptions"
              :key="item.ID"
              :label="item.fullname"
              :value="item.ID"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="specialForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="specialForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="specialForm.status.value === 5 && specialForm.ID > 0" type="danger" @click="handleDelete(specialForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveSpecial, removeSpecialById, getSpecialDetail } from '@/api/knowledge/special'
import { getEmployeeUsable } from '@/api/mechanism/employee'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: {Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      specialForm: {
        code: '',
        title: '',
        specialID:0,
        employeeID:0,
        keyword:[],
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      keywordFormHidden:true,
      keywordForm:{
        title:""
      },
      employeeOptions:[],
      specialFormRules: []
    }
  },
  created(options) {
    this.loadEmployeeList()
    let specialID = this.$route.query && this.$route.query.specialID
    if (specialID !== undefined) {
      specialID = parseInt(specialID)
      if (!isNaN(specialID)) {
        this.listLoading = true
        const formData = {
          'specialID': specialID
        }
        getSpecialDetail(formData).then(response => {
          if (response.status === 200) {
            this.specialForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    loadEmployeeList() {
      getEmployeeUsable().then(response => {
        if (response.status === 200) {
          this.employeeOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'special' })
    },
    handleKeywordConfirm:function(){
      let keyword = this.keywordForm.title;
      if (keyword) {
        this.specialForm.keyword.push(keyword);
      }
      this.keywordFormHidden = true;
      this.keywordForm.title = '';
    },
    handleKeywordHidden:function(){
      this.keywordFormHidden = false
      this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleKeywordRemove:function(index){
      this.specialForm.keyword.splice(index, 1);
    },
    draftData() {
      this.listLoading = true
      const _specialForm = this.specialForm
      _specialForm.status = 1
      if (_specialForm.ID > 0) {
        _specialForm.ID = _specialForm.ID
      }
      _specialForm.keyword = _specialForm.keyword.join('>>')
      saveSpecial(_specialForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'special' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _specialForm = this.specialForm
      _specialForm.status = 6
      if (_specialForm.ID > 0) {
        _specialForm.specialID = _specialForm.ID
      }
      _specialForm.keyword = _specialForm.keyword.join('>>')
      saveSpecial(_specialForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'special' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'specialID': row.ID
      }
      removeSpecialById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'special' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
